package logger

import (
	"fmt"
	"sync"
)

type logger []string

type logStore struct {
	logs logger
	num  int
}

var mutex = sync.Mutex{}

type logOptions interface {
	saveLog(log string)     // 保存日志
	saveLogs(logs []string) // 保存多条日志
	clearLogger()           // 清空日志存储器
	getLogs()               // 查询所有日志
}

func (l logStore) saveLog(log string) {
	mutex.Lock()
	defer mutex.Unlock()
	l.logs[l.num] = log
	l.num++
	fmt.Printf("hash: %p, num: %d\n", &l, l.num)
}

func (l logStore) saveLogs(logs []string) {
	mutex.Lock()
	defer mutex.Unlock()
	for _, log := range logs {
		l.logs[l.num] = log
		l.num++
	}
}

func (l logStore) clearLogger() {
	mutex.Lock()
	defer mutex.Unlock()
	l.logs = logger{}
	l.num = 0
}

func (l logStore) getLogs() (num int, logs []string) { // 会不会破环面向对象原则
	num = l.num
	logs = l.logs
	return
}
